

<form id="system-auth-form" lay-filter="system-auth-form" class="layui-form model-form admin-form" >
    <input name="id" type="hidden"/>
    <input name="createTime" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">权限名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入权限名称" type="text" class="layui-input" maxlength="50"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限标识</label>
        <div class="layui-input-block">
            <input name="authority" placeholder="请输入权限标识" type="text" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否为菜单</label>
        <div class="layui-input-block">
            <input type="radio" name="isMenu" lay-filter="system-auth-radio" value="1" title="是" checked/>
            <input type="radio" name="isMenu" lay-filter="system-auth-radio" value="0" title="否"/>
        </div>
    </div>
    <div class="layui-form-item menu-item">
        <label class="layui-form-label">菜单路径</label>
        <div class="layui-input-block">
            <input name="menuHref" placeholder="请输入菜单href" type="text" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item menu-item">
        <label class="layui-form-label">菜单地址</label>
        <div class="layui-input-block">
            <input name="menuUrl" placeholder="请输入菜单url" type="text" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item menu-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input name="menuIcon" placeholder="请输入菜单图标" type="text" class="layui-input"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input name="orderNum" placeholder="请输入排序号" lay-verify="required" type="number" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">父节点</label>
        <div class="layui-input-block" id="system-auth-parent-tree" >
            <input name="parentId"  style="display: none" type="hidden" />
            <input name="parentName" lay-verify="required" placeholder="--上级菜单--" readonly='readonly' type="text" class="layui-input"/>
        </div>
    </div>
    <div  style="display: none">
        <button class="layui-btn" lay-filter="system-auth-submit" id = "system-auth-submit" lay-submit>保存</button>
    </div>
</form>
<script>

    layui.use(['layer', 'form', 'admin','config','util'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var config = layui.config;
        var util = layui.util;
        var url = "system/authority/add";
        // 回显数据
        var authority = admin.getTempData('system-auth-obj');
        var authJson = admin.getTempData("system-auth-tree-json");

        form.render("radio","system-auth-form")

        if (authority) {
            url = "system/authority/update";
            if(authority.parentId){
                admin.putTempData("system-auth-parentId",authority.parentId);
                if(authority.parentId == "-1"){
                    authority.parentName = "根节点";
                }
                if(authJson){
                    for(var i=0;i<authJson.length;i++){
                        if(authJson[i].id == authority.parentId){
                            authority.parentName = authJson[i].name;
                            break;
                        }
                    }
                }
            }
            if (authority.isMenu == 0 || authority.isMenu == "0"){
                $("#system-auth-form [name='isMenu'][value='0']").attr("checked","checked");
                $("#system-auth-form .menu-item input").val("");
                $("#system-auth-form .menu-item").hide();
            }
            form.val('system-auth-form', authority);
            $('#system-auth-form input[name="createTime"]').val(util.toDateString(authority.createTime));
        }

        form.on('radio(system-auth-radio)',function(data){
            console.log(data)
            if(data.value == "1"){
                $("#system-auth-form .menu-item").show();
            }else{
                $("#system-auth-form .menu-item input").val("");
                $("#system-auth-form .menu-item").hide();
            }
        })

        // 表单提交事件
        form.on('submit(system-auth-submit)', function (data) {
            admin.req(url, data.field, function (data) {
                layer.msg("保存成功", {icon: 1});
                admin.putTempData('system-auth-formOk', true);  // 操作成功刷新表格
                layer.close(layer.curr_idex)
            },"post");
            return false;
        });
        $("#system-auth-parent-tree").click(function showTree() {
            admin.openTree({
                title: '上级菜单',
                path:"system/auth/menuTree.html",
                yes:function(index){
                    var treeObj = $.fn.zTree.getZTreeObj('system-auth-tree');
                    var nodes = treeObj.getCheckedNodes(true);
                    var ids = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        ids[i] = nodes[i].id;
                    }
                    $("#system-auth-form [name='parentId']").val(nodes[0].id);
                    $("#system-auth-form [name='parentName']").val(nodes[0].name);
                    layer.close(index)
                }
            });
        });
    });


</script>
